<?php
class Server{
    private $serv;

    public function __construct() {
        $this->serv = new swoole_server('0.0.0.0', 9501);
        $this->serv->set([
            'task_worker_num' => 2,
            'daemonize' => false,
        ]);
        $this->serv->on('Task', [$this,'onTask']);
        $this->serv->on('Finish',[$this ,'onFinish']);
        $this->serv->on('Start', [$this, 'onStart']);
        $this->serv->on('Connect', [$this, 'onConnect']);
        $this->serv->on('Receive', [$this, 'onReceive']);
        $this->serv->on('Close', [$this, 'onClose']);

        $this->serv->start();
    }

    public function onTask(swoole_server $serv, $task_id, $from_id,$data) {
        echo __FUNCTION__ . ": New AsyncTask[id=$task_id]" . PHP_EOL;
        $serv->finish($data);
    }

    public function onFinish(swoole_server $serv, $task_id, $data) {
        echo __FUNCTION__ . ": AsyncTask[$task_id] Finish: $data" . PHP_EOL;
    }

    public function onStart($serv) {
        echo "Start\n";
    }

    public function onConnect($serv, $fd, $from_id) {
        $serv->send($fd, "Hello {$fd}!");
    }

    public function onReceive(swoole_server $serv, $fd, $from_id, $data) {
        echo __FUNCTION__ . ": Get Message From Client {$fd}:{$data}\n";
        $serv->task($data);
        echo __FUNCTION__ . ": Receive {$fd} go on!\n";
    }

    public function onClose($serv, $fd, $from_id) {
        echo __FUNCTION__ . ": Client {$fd} close connection\n";
    }
}

$server = new Server();

?>
